package com.example.cd5160866.modelrecycleview.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.cd5160866.modelrecycleview.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by cd5160866 on 2016/10/8.
 */
public class Demo1Activity extends DemoBaseActivity {
    @BindView(R.id.rv_demo_base)
    RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        //        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //        LinearLayoutManager manager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(manager);

        MyAdapter adapter = new MyAdapter(data);
        recyclerView.setAdapter(adapter);
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        /**
         * 构造方法，传入view即我们listview的convertview
         *
         * @param itemView
         */
        public MyHolder(View itemView) {
            super(itemView);
        }

    }

    public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
        List<String> datas = new ArrayList<>();

        public MyAdapter(List<String> data) {
            this.datas = data;
        }

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
            //完成对holder的创建 将holder和view绑定起来，中间互用逻辑RecyclerView帮我们完成了
            return new MyHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, final int position) {
            //这里完成数据的绑定与事件的监听
            ImageView imageView = (ImageView) holder.itemView.findViewById(R.id.iv_item1);
//            imageView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(v.getContext(), "点击 " + position, Toast.LENGTH_SHORT).show();
//                }
//            });
            Picasso.with(holder.itemView.getContext()).load(datas.get(position)).into(imageView);
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }
    }

}
